package com.ruoyi.checkingin.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ruoyi.checkingin.service.IERpStandardService;
import com.ruoyi.common.core.domain.e_checkingin.ERpStandard;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.checkingin.mapper.ECheckTimeMapper;
import com.ruoyi.common.core.domain.e_checkingin.ECheckTime;
import com.ruoyi.checkingin.service.IECheckTimeService;

/**
 * 考勤记录Service业务层处理
 * 
 * @author JYW
 * @date 2024-01-30
 */
@Service
public class ECheckTimeServiceImpl implements IECheckTimeService 
{
    @Autowired
    private ECheckTimeMapper eCheckTimeMapper;

    @Autowired
    private IERpStandardService rpStandardService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询考勤记录
     * 
     * @param id 考勤记录主键
     * @return 考勤记录
     */
    @Override
    public ECheckTime selectECheckTimeById(Long id)
    {
        return eCheckTimeMapper.selectECheckTimeById(id);
    }

    /**
     * 查询考勤记录列表
     * 
     * @param eCheckTime 考勤记录
     * @return 考勤记录
     */
    @Override
    public List<ECheckTime> selectECheckTimeList(ECheckTime eCheckTime)
    {
        List<ECheckTime> eCheckTimes = eCheckTimeMapper.selectECheckTimeList(eCheckTime);
       if (!eCheckTimes.isEmpty()){
           List<ECheckTime> collect = eCheckTimes.stream().peek(checkTime -> {
               checkTime.setRemark(SecurityUtils.getUsername());
               if (checkTime.getSignInStatus() != null) {
                   ERpStandard eRpStandard = rpStandardService.selectERpStandardById(checkTime.getSignInStatus());
                   checkTime.setCreateBy(eRpStandard == null ? "正常" : eRpStandard.getRpName());
               }
               if (checkTime.getSignOutStatus() != null) {
                   ERpStandard eRpStandard = rpStandardService.selectERpStandardById(checkTime.getSignOutStatus());
                   checkTime.setUpdateBy(eRpStandard == null ? "正常" : eRpStandard.getRpName());
               }
           }).collect(Collectors.toList());
           return collect;
       }
        return eCheckTimes;
    }

    /**
     * 新增考勤记录
     * 
     * @param eCheckTime 考勤记录
     * @return 结果
     */
    @Override
    public int insertECheckTime(ECheckTime eCheckTime)
    {
        eCheckTime.setCreateTime(DateUtils.getNowDate());
        return eCheckTimeMapper.insertECheckTime(eCheckTime);
    }

    /**
     * 修改考勤记录
     * 
     * @param eCheckTime 考勤记录
     * @return 结果
     */
    @Override
    public int updateECheckTime(ECheckTime eCheckTime)
    {
        eCheckTime.setUpdateTime(DateUtils.getNowDate());
        return eCheckTimeMapper.updateECheckTime(eCheckTime);
    }

    /**
     * 批量删除考勤记录
     * 
     * @param ids 需要删除的考勤记录主键
     * @return 结果
     */
    @Override
    public int deleteECheckTimeByIds(Long[] ids)
    {
        return eCheckTimeMapper.deleteECheckTimeByIds(ids);
    }

    /**
     * 删除考勤记录信息
     * 
     * @param id 考勤记录主键
     * @return 结果
     */
    @Override
    public int deleteECheckTimeById(Long id)
    {
        return eCheckTimeMapper.deleteECheckTimeById(id);
    }

    @Override
    public Map<String, Object> homePageUser() {
        Map<String,Object> map = new HashMap<>();
        //总人数
//        List<SysUser> sysUsers = sysUserService.selectUserList(new SysUser());
        int size =sysUserService.selectSum();
        long count = 0;
        long count2 = 0;
        long count3 = 0;
        long count4 = 0;
        long count5 = size;
        ECheckTime checkTime = new ECheckTime();
        String s = DateUtils.parseDateToStr("yyyy-MM-dd", new Date());
        checkTime.setCreateTime(DateUtils.dateTime("yyyy-MM-dd",s));
        List<ECheckTime> eCheckTimes = eCheckTimeMapper.selectECheckTimeList(checkTime);
        //今日迟到人数
      if (!eCheckTimes.isEmpty()){
           count = eCheckTimes.stream().filter(eCheckTime -> eCheckTime.getSignInStatus().equals(1L)).count();
          //今日早退人数
           count2 = eCheckTimes.stream().filter(eCheckTime -> eCheckTime.getSignInStatus().equals(2L)).count();
          //今日矿工人数
           count3 = eCheckTimes.stream()
                  .filter(eCheckTime -> eCheckTime.getSignInStatus().equals(3L) || eCheckTime.getSignOutStatus().equals(3L))
                  .count();
          //今日状态异常人数
           count4 = eCheckTimes.stream()
                  .filter(eCheckTime ->
                          (eCheckTime.getSignInStatus()!=null && eCheckTime.getSignInStatus().equals(-1L)) ||
                                  (eCheckTime.getSignOutStatus()!=null && eCheckTime.getSignOutStatus().equals(-1L))
                  )
                  .count();
          //今日状态正常人数
           count5 = size-count4;
      }
        map.put("userSum",size);
        map.put("checkinOut",count);
        map.put("checkinLeave",count2);
        map.put("checkinAbsent",count3);
        map.put("checkinUnusual",count4);
        map.put("checkinNormal",count5);
        return map;
    }
}
